# 导入依赖
import pymysql

# 1、创建数据库连接
con = pymysql.connect(
    host="master",
    port=3306,
    user="root",
    password="123456",
    database="shujia"
)

# 2、使用游标查询数据
cursor = con.cursor()

# 3、删除数据，执行sql返回影响的行数
# flog = cursor.execute("delete from students")

# 2、插入数据,返回受影响的行数
# num = cursor.execute("insert into students values('1500100001','施笑槐',22,'女','文科六班')")
#
# print(num)

# 3、更新数据,返回受影响的行数,如果返回0代表没有数据受影响
# up_num = cursor.execute("update students set age=25 where id='1500100002'")
# print(up_num)


# 4、查询数据，只返回查询到的数据量
num = cursor.execute("select * from scores")
print(num)

# 获取所有查询到的数据，返回一个元组
# scores = cursor.fetchall()
#
# # 循环获取数据
# for sid, cid, score in scores:
#     print(sid, cid, score)


# 获取一行，
score = cursor.fetchone()

# 当不为空时获取下一行
while score is not None:
    print(score)

    # 获取下一行
    score = cursor.fetchone()

# 提交事务
con.commit()

# 关闭连接
con.close()
